package org.common4j2ee.s2shs.web.user;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.common4j2ee.s2shs.dao.utils.HibernateUtils;
import org.common4j2ee.s2shs.dao.utils.Page;
import org.common4j2ee.s2shs.dao.utils.PropertyFilter;
import org.common4j2ee.s2shs.entity.user.Role;
import org.common4j2ee.s2shs.entity.user.Users;
import org.common4j2ee.s2shs.service.user.RoleManager;
import org.common4j2ee.s2shs.service.user.UserManager;
import org.common4j2ee.s2shs.service.utils.ServiceException;
import org.common4j2ee.s2shs.web.utils.CrudActionSupport;
import org.common4j2ee.s2shs.web.utils.PropertyFilterWeb;
import org.common4j2ee.s2shs.web.utils.Struts2Utils;
import org.springframework.beans.factory.annotation.Autowired;


@Namespace("/user")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "user!list.action", type = "redirect") })
public class UserAction extends CrudActionSupport<Users> {

	private Users user; 
	
	private Long id;
	
	private List<Long> checkedRoleIds;
	
	private Page<Users> page = new Page<Users>(10);//每页5条记录

	@Autowired
	UserManager userManager;
	
	@Autowired
	RoleManager roleManager;

	private static final long serialVersionUID = 633445360169800367L;
	
	
	//-- CRUD Action 函数 --//
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilterWeb.buildFromHttpRequest(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		page = userManager.searchUser(page, filters);
		return SUCCESS;
	}
	
	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	@Override
	public Users getModel() {
		return user;
	}

	@Override
	public String input() throws Exception {
		checkedRoleIds = user.getRoleIds();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		HibernateUtils.mergeByCheckedIds(user.getRoleList(), checkedRoleIds, Role.class);

		userManager.saveUser(user);
		addActionMessage("保存用户成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		try {
			userManager.deleteUser(id);
			addActionMessage("删除用户成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage("删除用户失败");
		}
		return RELOAD;
	}
	
	//-- 其他Action函数 --//
	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkLoginName() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newLoginName="";
		String oldLoginName="";
		try {
			newLoginName = URLDecoder.decode(request.getParameter("loginName"),"UTF-8");
			oldLoginName = URLDecoder.decode(request.getParameter("oldLoginName"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			
		}

		if (userManager.isLoginNameUnique(newLoginName, oldLoginName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		//因为直接输出内容而不经过jsp,因此返回null.
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id==null){
			user=new Users();
		}else{
			user=userManager.getUser(id);
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Page<Users> getPage() {
		return page;
	}

	public void setPage(Page<Users> page) {
		this.page = page;
	}

	public List<Long> getCheckedRoleIds() {
		return checkedRoleIds;
	}

	public void setCheckedRoleIds(List<Long> checkedRoleIds) {
		this.checkedRoleIds = checkedRoleIds;
	}

	
	public List<Role> getAllRoleList() {
		return roleManager.getAllRole();
	}
	

}
